package nz.ac.massey.doi_information_extractor.gui;

import java.awt.Component;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class DoiDialog extends JDialog implements ActionListener, KeyListener{

	private static DoiDialog dialog;
	private static String doi;
	private JButton okButton;
	private JTextField doiTextField;

	public DoiDialog(Frame frame){
		super(frame, "Enter DOI", true);

		init();
	}

	public static String showDialog(Component frameComp){
		dialog = new DoiDialog(JOptionPane.getFrameForComponent(frameComp));
		doi = null;
		dialog.setVisible(true);
		dialog = null;
		return doi;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String text = doiTextField.getText();

		if(text != null){
			doi = text;
		}else{
			text = "";
		}
		dialog.setVisible(false);
	}

	private void init(){

		JPanel mainPanel = new JPanel();
        doiTextField = new JTextField();
        doiTextField.addKeyListener(this);


        okButton = new JButton("OK");
        okButton.addActionListener(this);

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(doiTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 266, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(doiTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(okButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

		getContentPane().add(mainPanel);
		pack();
	}

	@Override
	public void keyPressed(KeyEvent arg0) {}

	@Override
	public void keyReleased(KeyEvent arg0) {
		if(arg0.getKeyChar()== KeyEvent.VK_ENTER){
			actionPerformed(null);
		}
	}

	@Override
	public void keyTyped(KeyEvent arg0) {}
}
